﻿/*
  Copyright (c) 2011, Mikheev Rostislav
  Copyright (c) 2011, FreshFarsh team

  Steel Engine source code is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/**
  @author Mikheev Rostislav <hacenator@gmail.com>
  @date 23.03.2011
  @brief Python script executor source code
*/
#include "scripts/script.h"

namespace _STEEL_NAMESPACE {

Script::Script(void)
  : _loaded(false),
  _buffer(NULL)
{

}

Script::Script(const char* filename)
  : _loaded(false),
  _buffer(NULL)
{
  LoadFromFile(filename);
}

Script::~Script(void) {
  delete[] _buffer;
}

bool Script::LoadFromFile(const char* filename) {
  if (!FileExists(filename)) {
		_console->Warnf("Script::loadFromFile() file '%s' not found\n", filename);
		return false;
	}

	IOFile file(filename, "r");
	int size = file.Size();

	_console->Warnf("Script::LoadFromFile() Loading '%s' (%dbytes)\n", filename, size);

  delete[] _buffer;
	_buffer = new char[size];

	int readed = file.Read(_buffer);
	_buffer[readed] = 0x0;

	#ifdef _DEBUG
	//_console->infof("[DEBUG]\n%s\n[DEBUG]\n", _buffer);
	#endif

	file.Close();

	// Script sucsessfull loaded from file
	_loaded = true;

	return true;
}

///-----------------------------------------------------
bool Script::Execute() {
	if (_loaded)
		if (PyRun_SimpleString(_buffer) != 0)
      _console->Warn("Script::Execute() Python script execution error.\n");

	return true;
}

}
